import {message} from 'antd';
import {formatMessage} from './intl';

export const newOrUpdateOrderValidator = data => {
    //增加判断必填字段
    const emptyErrors = [];
    if(!data || !data.brandId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.brand',
            defaultMessage: '品牌'
        }));
        
    if(!data || !data.dealerId)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.dealerId',
            defaultMessage: '总代'
        }));

    if(!data || !data.code)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.code',
            defaultMessage: '信用证号码'
        }));

    if(!data || !data.issueCountry)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.issueCountry',
            defaultMessage: '开证国家'
        }));
    
    if(!data || !data.issueBank)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.issueBank',
            defaultMessage: '开证银行'
        }));
    if(!data || !data.lcPayMentDays)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.lcPayMentDays',
            defaultMessage: '信用证账期'
        }));

    if(!data || !data.issuePrice)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.issuePrice',
            defaultMessage: '开证金额'
        }));

    if(!data || !data.issueDate)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.issueDate',
            defaultMessage: '开证日期'
        }));
    
    if(!data || !data.lastLoadDate)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.lastLoadDate',
            defaultMessage: '最迟装船日期'
        }));

    if(!data || !data.validityDate)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.validityDate',
            defaultMessage: '有效期'
        }));

    if(!data || !data.lcKind)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.lcKind',
            defaultMessage: '信用证类型'
        }));
    
    if(!data || !data.currency)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.currency',
            defaultMessage: '币种'
        }));
    
    if(!data || !data.isBatches)
        emptyErrors.push(formatMessage({
            id: 'validator.empty.isBatches',
            defaultMessage: '是否分批'
        }));
    
    if(emptyErrors.length > 0) {
        message.warning(`${emptyErrors.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    return true;
};

export const receiveOrderValidator = details => {
    const requiredFields = [];
    const newDetails = details.map(d => d.invoiceCode);
    if(details.some(d => !d.invoiceCode || new Set(newDetails).size !== newDetails.length))
        requiredFields.push(
            formatMessage({
                id: 'validator.invoiceCode',
                defaultMessage: '发票号'
            }));

    if(details.some(d => !d.cashInPrice))
        requiredFields.push(
            formatMessage({
                id: 'validator.cashInPrice',
                defaultMessage: '到款金额'
            }));

    if(details.some(d => !d.cashInDate))
        requiredFields.push(
            formatMessage({
                id: 'validator.cashInDate',
                defaultMessage: '到款日期'
            }));

    if(requiredFields.length > 0) {
        message.warning(`${requiredFields.join('，')}${formatMessage({
            id: 'validator.required',
            defaultMessage: '必填！'
        })}`);
        return false;
    }
    return true;
};
